﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Data;

namespace map.member.dialogs
{
    public partial class ItemSeller_cateitem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (AjaxFunction ));
            //AliApi.appUserId = "maxfara@163.com";
            if (!IsPostBack)
            {
                this.NoDataDiv.Visible = false;
                BindDropDownList();
                BindMainWebPager();
            }
            else
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "initframe", "<script type=\"text/javascript\">if(initFrameHeight)initFrameHeight();</script>", false);
        }

        #region 辅助方法

        public string GetCateListUlHtml(string cidsStr,string itemRECID)
        {
            string resulthtml = string.Empty;
            string[] cidList = cidsStr.Trim (",".ToCharArray ()).Split(",".ToCharArray ());
            if (cidList.Length > 0)
            {
                string filter = string.Empty;
                
                int index = 0;
                for (int i = 0; i < cidList.Length; i++)
                {
                    if (index > 0)
                        filter += " or ";
                    if (cidList[i].Trim() != "")
                    {
                        filter += " taobaocid='" + cidList[i].Trim() + "' ";
                        index++;
                    }
                }
                if (filter.Trim() != string.Empty)
                {
                    resulthtml =string.Empty ;
                    taobaoItemCat taobaoitemcate = taobaoItemCat.Find(filter);
                    for (int i = 0; i < taobaoitemcate.Count; i++)
                    {
                        string spanInnerText = taobaoitemcate.taobaoname.Value.Trim();
                        if (taobaoitemcate.taobaoparentcid.Value != "0")
                        {
                            taobaoItemCat parentitemcate = taobaoItemCat.Find("taobaocid='"+taobaoitemcate.taobaoparentcid .Value +"'");
                            if (parentitemcate.Count > 0)
                                spanInnerText = parentitemcate.taobaoname .Value+">>"+spanInnerText  ;
                        }
                        resulthtml += "<li id=\"" + taobaoitemcate.taobaocid.Value + "\"> <span id=\"" + itemRECID + "\">" + spanInnerText + "</span><a href=\"#\" onclick=\"removeItemCate(this)\">&nbsp;</a></li>";
                        taobaoitemcate.Next();
                    }                        
                }
            }
            resulthtml = "<ul>"+resulthtml +"</ul>";
            return resulthtml;
        }

        private void BindDropDownList()
        {
            string parentCateId = string.Empty ;
            string selectedCateId = "-2";
            if (Request["itemcateid"] != null)
                selectedCateId = Request["itemcateid"];
            if (Request["parentcid"] != null)
                parentCateId = Request["parentcid"];
            if (parentCateId == string.Empty)
                parentCateId = selectedCateId;

            this.SelItemCateDropDownList0.Items.Clear();
            this.SelItemCateDropDownList1.Items.Clear();
            this.OprItemCateDropDownList0.Items.Clear();
            ListItem listItem = new ListItem("全部分类", "-1");
            if (listItem.Value == parentCateId)
                listItem.Selected = true;
            this.SelItemCateDropDownList0.Items.Add(listItem);
            listItem = new ListItem("未分类", "-2");
            if (listItem.Value == parentCateId)
                listItem.Selected = true;
            this.SelItemCateDropDownList0.Items.Add(listItem);
            taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaoparentcid='0' and taobaouserid='" + AliApi.appUserId + "'");
            if (taobaoitemcate.Count > 0)
            {
                ListItem oprlistitem;
                for (int i = 0; i < taobaoitemcate.Count; i++)
                {
                    listItem = new ListItem(taobaoitemcate.taobaoname.Value, taobaoitemcate.taobaocid.Value);
                    if (listItem.Value == parentCateId)
                        listItem.Selected = true;
                    this.SelItemCateDropDownList0.Items.Add(listItem);

                    oprlistitem = new ListItem(taobaoitemcate.taobaoname.Value, taobaoitemcate.taobaocid.Value);                    
                    this.OprItemCateDropDownList0.Items.Add(oprlistitem);
                    
                    taobaoitemcate.Next();
                }
            }

            this.OprItemCateDropDownList0.Enabled  = false;
            this.OprItemCateDropDownList1.Visible = false;
            this.SelItemCateDropDownList1.Visible = false;                                               
            this.OprNameDropDownList.SelectedIndex = 0;

            if (this.SelItemCateDropDownList0.SelectedIndex < 0)
                this.SelItemCateDropDownList0.SelectedIndex = 1;
            if (Request["parentcid"] != null && (!IsPostBack ))
            {
                this.SelItemCateDropDownList0_SelectedIndexChanged(this.SelItemCateDropDownList0, EventArgs.Empty);
            }
        }

        public string GetItemUrl(string itemRecid)
        {
            return ResolveUrl("~/member/i.aspx?id=" + itemRecid);
        }

        private void BindMainWebPager()
        {
            if (AliApi.appUserId != string.Empty)
            {
                if (this.SelItemCateDropDownList1.Visible == true && this.SelItemCateDropDownList1.SelectedValue == string.Empty)
                {
                    taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaoparentcid='"+this.SelItemCateDropDownList0.SelectedValue +"'");
                    BindMainWebPager(taobaoitemcate);
                    return;
                }

                string searchKeys = this.searchKeysTextBox.Text;
                if (searchKeys.Trim() == string.Empty)
                    searchKeys = "%";
                else                                                                                                    
                    searchKeys = "%" +searchKeys + "%";
                string cateId ="-1" ;
                if (this.SelItemCateDropDownList1.Visible )
                    cateId = this.SelItemCateDropDownList1.SelectedValue;
                else
                    cateId = this.SelItemCateDropDownList0.SelectedValue;                
                string filter = "taobaouserid='" + AliApi.appUserId + "' and taobaotitle like '" + searchKeys + "' ";
                if (cateId == "-2")
                    filter += " and taobaoseller_cids=''";
                else
                {
                    if (cateId != "-1")
                    {
                        filter += " and taobaoseller_cids like '%" + cateId + "%'";
                    }
                }

                taobaouseritems useritems = taobaouseritems.Find(filter);
                NoDataDiv.Visible = (useritems.Count > 0) ? false : true;
                this.MainWebPager.DataSource = useritems.MyTable;
                this.MainWebPager.DataBind();
            }
            else
            {
                this.NoDataDiv.Visible = true;
            }
        }

        private void BindMainWebPager(taobaoItemCat taobaoitemcate)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string cidStr ="(";
                for (int i = 0; i < taobaoitemcate.Count; i++)
                {
                    if(i>0)
                        cidStr += " or ";
                    cidStr += " taobaoseller_cids like '%" + taobaoitemcate.MyTable.Rows[i] ["taobaocid"].ToString () + "%' ";
                }
                cidStr += ")";
                if (taobaoitemcate.Count >0)
                    cidStr = " and " + cidStr;

                string searchKeys = this.searchKeysTextBox.Text;
                if (searchKeys.Trim() == string.Empty)
                    searchKeys = "%";
                else
                    searchKeys = "%" + searchKeys + "%";
                
                string filter = "taobaouserid='" + AliApi.appUserId + "' and taobaotitle like '" + searchKeys + "' ";
                if (taobaoitemcate.Count > 0)
                    filter += cidStr;

                taobaouseritems useritems = taobaouseritems.Find(filter);
                NoDataDiv.Visible = (useritems.Count > 0) ? false : true;
                this.MainWebPager.DataSource = useritems.MyTable;
                this.MainWebPager.DataBind();
            }
            else
            {
                this.NoDataDiv.Visible = true;
            }
        }
        #endregion

        #region 下拉框 事件
        protected void OprNameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OprItemCateDropDownList0.Enabled  = (this.OprNameDropDownList.SelectedIndex > 0) ?true  : false ;
            if (this.OprNameDropDownList.SelectedIndex > 0)
                OprItemCateDropDownList0_SelectedIndexChanged(this.OprItemCateDropDownList0,EventArgs.Empty );
        }      
                                                                            

        protected void SelItemCateDropDownList0_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelItemCateDropDownList1.Visible = false;
            this.SelItemCateDropDownList1.Items.Clear();
            string cateid0 = this.SelItemCateDropDownList0.SelectedValue;
            if (cateid0 != "-1" && cateid0 != "-2")
            {
                taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaoparentcid='" + cateid0 + "' and taobaouserid='" + AliApi.appUserId + "'");
                if (taobaoitemcate.Count > 0)
                {
                    string selectedCateId = string.Empty;
                    if (Request["itemcateid"] != null && Request["parentcid"] != null)
                        selectedCateId = Request["itemcateid"];

                    ListItem listItem = new ListItem("选择二级分类", "");
                    this.SelItemCateDropDownList1.Items.Add(listItem);
                    for (int i = 0; i < taobaoitemcate.Count; i++)
                    {
                        listItem = new ListItem(taobaoitemcate.taobaoname.Value, taobaoitemcate.taobaocid.Value);
                        if (taobaoitemcate.taobaocid.Value == selectedCateId && taobaoitemcate.taobaoparentcid.Value == Request["parentcid"] && (!IsPostBack))
                            listItem.Selected = true;
                        this.SelItemCateDropDownList1.Items.Add(listItem);
                        taobaoitemcate.Next();
                    }
                    if(this.SelItemCateDropDownList1 .SelectedIndex <0)
                        this.SelItemCateDropDownList1.SelectedIndex = 0;
                    this.SelItemCateDropDownList1.Visible = true;

                    BindMainWebPager(taobaoitemcate);
                }
                else
                {
                    BindMainWebPager();
                }
            }
            else
            {
                BindMainWebPager();
            }
        }

        protected void SelItemCateDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMainWebPager();
        }

        protected void OprItemCateDropDownList0_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OprItemCateDropDownList1.Visible = false;
            this.OprItemCateDropDownList1.Items.Clear();
            if (AliApi.appUserId != string.Empty)
            {
                taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaoparentcid='" + this.OprItemCateDropDownList0.SelectedValue  + "' and taobaouserid='" + AliApi.appUserId + "'");
                if (taobaoitemcate.Count > 0)
                {
                    for (int i = 0; i < taobaoitemcate.Count; i++)
                    {
                        ListItem listItem = new ListItem(taobaoitemcate.taobaoname.Value, taobaoitemcate.taobaocid.Value);
                        this.OprItemCateDropDownList1.Items.Add(listItem);
                        taobaoitemcate.Next();
                    }
                    this.OprItemCateDropDownList1.SelectedIndex = 0;
                    this.OprItemCateDropDownList1.Visible = true;
                }
            }
        }       
        #endregion

        #region 选择事件
        protected void SelectOthersCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.MainDataList.Items.Count; i++)
            {
                CheckBox checkbox = this.MainDataList.Items[i].FindControl("ItemSelectCheckBox") as CheckBox;
                checkbox.Checked = !checkbox.Checked;
            }
        }

        protected void SelectAllCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.MainDataList.Items.Count; i++)
            {
                CheckBox checkbox = this.MainDataList.Items[i].FindControl("ItemSelectCheckBox") as CheckBox;
                checkbox.Checked = this.SelectAllCheckBox.Checked;
            }
        }
        #endregion

        protected void CateOprOkButton_Click(object sender, EventArgs e)
        {
            string oprName = this.OprNameDropDownList.SelectedValue;
            if (AliApi.appUserId != string.Empty)
            {                         
                string selectedCateId = string.Empty;
                if (this.OprItemCateDropDownList1.Visible)
                    selectedCateId = (this.OprItemCateDropDownList1 .SelectedIndex >=0)?this.OprItemCateDropDownList1.SelectedValue:this.OprItemCateDropDownList1 .Items[0].Value ;
                else
                    selectedCateId = this.OprItemCateDropDownList0.SelectedValue;
                if (selectedCateId.Trim () == string.Empty)
                    return;

                taobaouseritems useritems = taobaouseritems.Find("taobaouserid='" + AliApi.appUserId + "'");
                for (int i = 0; i < this.MainDataList.Items.Count; i++)
                {
                    CheckBox checkbox = this.MainDataList.Items[i].FindControl("ItemSelectCheckBox") as CheckBox;
                    if (checkbox.Checked)
                    {
                        int index = useritems.IndexOf ("recid="+this.MainDataList.DataKeys [i].ToString ());
                        useritems.CurrentIndex = index;
                        if (oprName == "Add")
                        {
                            string oldCatesStr = useritems.taobaoseller_cids.Value;
                            oldCatesStr = oldCatesStr.Trim();
                            if (oldCatesStr.IndexOf(selectedCateId) < 0)
                            {
                                if (oldCatesStr == string.Empty)
                                    oldCatesStr += ",";
                                if (oldCatesStr.Substring(oldCatesStr.Length - 1, 1) != ",")
                                    oldCatesStr += ",";
                                oldCatesStr += selectedCateId + ",";
                                useritems.taobaoseller_cids.Value = oldCatesStr;
                                useritems.Modify();
                            } 
                        }
                        else if (oprName == "Move")
                        {
                            useritems.taobaoseller_cids.Value = "," + selectedCateId + ",";
                            useritems.Modify();
                        }
                    }
                }

                BindMainWebPager();
            }
        }

        protected void searchItemButton_Click(object sender, EventArgs e)
        {
            BindMainWebPager();
        }

        protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            this.MainWebPager.CurrentPageIndex = e.NewPageIndex;
            BindMainWebPager();
        }

        

        
    }
}
